<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ESP32 MQTT 配置</title>
    <style>
        :root {
            --primary-color: #4CAF50;
            --secondary-color: #2E7D32;
            --error-color: #f44336;
            --text-color: #333;
            --light-gray: #f5f5f5;
            --white: #ffffff;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: var(--light-gray);
            color: var(--text-color);
            line-height: 1.6;
            padding: 20px;
        }

        .container {
            max-width: 500px;
            margin: 0 auto;
            background: var(--white);
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: var(--primary-color);
            text-align: center;
            margin-bottom: 25px;
            font-weight: 500;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
        }

        select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            transition: border 0.3s;
        }

        select:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            transition: border 0.3s;
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
        }

        .btn {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: var(--secondary-color);
        }

        .btn-secondary {
            background-color: #6c757d;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
        }

        .btn-block {
            width: 100%;
        }

        .nav-buttons {
            display: flex;
            gap: 10px;
        }

        .logo {
            text-align: center;
            margin-bottom: 20px;
        }

        .logo img {
            max-width: 80px;
        }

        .status {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
        }

        .success {
            background-color: #E8F5E9;
            color: var(--secondary-color);
        }

        .error {
            background-color: #FFEBEE;
            color: var(--error-color);
        }

        .current-values {
            margin: 20px 0;
            padding: 15px;
            background-color: #E8F5E9;
            border-radius: 4px;
        }

        .current-values h3 {
            margin-top: 0;
            color: var(--secondary-color);
        }

        @media (max-width: 600px) {
            .container {
                padding: 20px;
            }
        }

        .ud-logo {
            width: 80px;
            height: 80px;
            margin: 0 auto 15px;
            background: linear-gradient(135deg, #4CAF50 0%, #2196F3 100%);
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 36px;
            font-weight: bold;
            font-family: 'Arial', sans-serif;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
<div class="container">
    <div class="logo">
        <div class="ud-logo">UD</div>
        <h1>MQTT配置</h1>
        <div class="nav-buttons">
            <a href="/main" class="btn btn-secondary">返回Wi-Fi设置</a>
        </div>
    </div>
    <div class="current-values">
        <h3>当前配置</h3>
        <p><span class="status-label">设备类型:</span> None</p>
        <p><span class="status-label">MQTT IP:</span> None</p>
        <p><span class="status-label">MQTT PORT:</span> 1883</p>
        <p><span class="status-label">MQTT USER:</span> None</p>
        <p><span class="status-label">MQTT PASS:</span> None</p>
        <p><span class="status-label">MQTT FULL TOPIC:</span> None/</p>
    </div>

    <form id="mqttForm">

        <div class="form-group">
            <label for="device_type">设备类型</label>
            <select id="device_type" name="device_type" required>
                <option value="">请选择设备类型</option>
                <option value="rongyang">溶氧传感器</option>
                <option value="andan">氨氮传感器</option>
                <option value="PH">PH传感器</option>
            </select>
        </div>
        <div class="form-group">
            <label for="mqtt_server">MQTT IP</label>
            <input type="text" id="mqtt_server" name="mqtt_server" placeholder="输入MQTT IP 必填" required>
        </div>

        <div class="form-group">
            <label for="mqtt_port">MQTT PORT</label>
            <input type="text" id="mqtt_port" name="mqtt_port" placeholder="输入MQTT PORT 默认端口 1883">
        </div>
        <div class="form-group">
            <label for="mqtt_user">MQTT USER</label>
            <input type="text" id="mqtt_user" name="mqtt_user" placeholder="输入MQTT USER">
        </div>
        <div class="form-group">
            <label for="mqtt_password">MQTT PASS</label>
            <input type="password" id="mqtt_password" name="mqtt_password" placeholder="���入MQTT PASS">
        </div>
        <div class="form-group">
            <label for="mqtt_topic">MQTT TOPIC前缀</label>
            <input type="text" id="mqtt_topic" name="mqtt_topic" placeholder="输入MQTT TOPIC前缀 默认��缀 ud/device">
        </div>
        <div class="form-group">
            <label for="topic">MQTT TOPIC主题</label>
            <input type="text" id="topic" name="topic" placeholder="输入MQTT TOPIC">
        </div>

        <button type="submit" class="btn submitBtn">保存配置</button>
    </form>

</div>
</body>

<script>
    // 修改表单提��为JSON格式
    let submitBtn = document.querySelector('.submitBtn')
    submitBtn.addEventListener('click', async (e) => {
        e.preventDefault();
        // 收集表单数据
        const formData = {
            mqtt_server: document.getElementById('mqtt_server').value,
            mqtt_port: document.getElementById('mqtt_port').value,
            mqtt_user: document.getElementById('mqtt_user').value,
            password: document.getElementById('mqtt_password').value,
            mqtt_topic: document.getElementById('mqtt_topic').value,
            topic: document.getElementById('topic').value
        };
        console.log(submitBtn, '----')
        try {
            // 发送JSON数据
            const response = await fetch('/mqttsave', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(formData)
            });

            if (response.ok) {
                alert('配置保存成功！���备将重启');
                setTimeout(() => {
                    window.location.reload();
                }, 2000);
            } else {
                const error = await response.text();
                alert('保���失败: ' + error);
            }
        } catch (err) {
            alert('��络错误: ' + err.message);
        }
        console.log("表单提交被拦截");
    });

    async function get_data() {
        console.log('xxxxxx')
        let currentVal = document.querySelector('.current-values')

        let resultData = await fetch('/mqttdata', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(formData)
        });

        let str = `
     <h3>当前配置</h3>
        <p><span class="status-label">设备类型:</span> ${resultData.device_type}</p>
        <p><span class="status-label">MQTT IP:</span> ${resultData.device_type}</p>
        <p><span class="status-label">MQTT PORT:</span> ${resultData.device_type}</p>
        <p><span class="status-label">MQTT USER:</span> ${resultData.device_type}</p>
        <p><span class="status-label">MQTT PASS:</span> ${resultData.device_type}</p>
        <p><span class="status-label">MQTT FULL TOPIC:</span> ${resultData.device_type}/</p>
    `
        currentVal.innerHTML = str
    }

    get_data()


</script>

</html>
